package com.pj.wisdomlogistics.ui.activity;

import android.support.v4.content.ContextCompat;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.Log;

import butterknife.BindView;
import butterknife.OnClick;

public class TakeOutH5Activity extends BaseActivity {
    @BindView(R.id.webview)
    WebView mWebView;
    @BindView(R.id.tv_title)
    TextView mTvTitle;

    private int place; //政府大楼1双和园2
    private int type;//面食0熟食1

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this , R.color.text_blue));
        place = getIntent().getIntExtra("place" , -1);
        type = getIntent().getIntExtra("type" , -1);


        if(type == 1){
            mTvTitle.setText("一周熟食餐单");
        }else{
            mTvTitle.setText("一周面食餐单");
        }

        setWebView(place);
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_take_out_h5;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @OnClick(R.id.iv_back)
    public void onClick(){
        ActivityManagerUtil.create().finishActivity();
    }

    private void setWebView(final int place){
        WebSettings settings = mWebView.getSettings();

        //设置与js交互的权限
        settings.setJavaScriptEnabled(true);
        //设置允许js弹窗
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        //设置支持缩放
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);
        //自适应
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDomStorageEnabled(true);

        //载入js
        if(type == 1){
            //熟食
            mWebView.loadUrl("http://58.213.150.99:8010/zhhq/index.html");
            //mWebView.loadUrl("http://www.wsspha.cn/zhhq/");
        }else{
            //面食
            mWebView.loadUrl("http://58.213.150.99:8010/zhhq/food.html");
        }


        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.e("Tag" , "place:" + place + "----" + "type:" + type);
                mWebView.loadUrl("javascript:getPlace(" + place + ")");
            }
        });

    }


}


